﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class About : System.Web.UI.Page
{
    BankDataDataContext db = new BankDataDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
        string creditIdString = null;
        if (Session["creditMenuId"] != null)
        {
            creditIdString = Session["creditMenuId"].ToString();
        }
        if (creditIdString != null)
        {
            if (creditIdString != "-1")
            {
                int creditId = Convert.ToInt32(creditIdString);
                var credits = from credit in db.Credits where credit.CreditID == creditId select credit;
                string tableCurrency = "";
                string tableCreditPercentage = "";
                Credit selectedCredit = credits.First();
                CreditName.Text = selectedCredit.Name;
                CreditDescription.Text = selectedCredit.Description;
                CreditCurrency.Text = "";
                foreach (CreditCurrency currency in selectedCredit.CreditCurrencies)
                {
                    CreditCurrency.Text += (currency.Currency.Name + "; ");
                    tableCurrency += ("<td>" + currency.Currency.Name + "</td>");
                    tableCreditPercentage += ("<td>" + currency.Percentage.ToString() + "%</td>");
                }
                CreditFacilitiesFormLabel.Text = "";
                foreach (CreditCreditFacilitiesForm from in selectedCredit.CreditCreditFacilitiesForms)
                {
                    CreditFacilitiesFormLabel.Text += from.CreditFacilitiesForm.Name + "</br>";
                }
                MinCreditSize.Text = selectedCredit.MinValue.ToString() + " USD";
                MaxCreditSize.Text = selectedCredit.MaxValue.ToString() + " USD";
                CreditCommisionPercentage.Text = selectedCredit.CommissionPercentage.ToString() + "%";
                MinTerm.Text = selectedCredit.MinimumTerm.ToString() + " мес.";
                MaxTerm.Text = selectedCredit.MaximumTerm.ToString() + " мес.";
                CreditCurrencies.Text = tableCurrency;
                CreditPercentage.Text = tableCreditPercentage;
            }
        }
        else
        {
            Response.Redirect("~/Default.aspx");
        }
    }

    protected void SendCreditRequest_Click(object sender, EventArgs e)
    {
        string creditId = null;
        if (Session["creditMenuId"] != null)
        {
            creditId = Session["creditMenuId"].ToString();
        }
        MembershipUser user = Membership.GetUser();
        Session.Add("creditId", creditId);
        Session.Remove("creditMenuId");
        if (user != null && Roles.IsUserInRole(user.UserName, "User"))
        {
            var profile = from users in db.UserProfiles where users.userId == (Guid)user.ProviderUserKey select users;
            UserProfile userProfile = profile.First();
            Session.Add("clientId", userProfile.ProfileId.ToString());
            Response.Redirect("~/RequestCredit.aspx");
        }
        else
        {
            Response.Redirect("~/RequestCredit.aspx");
        }
    }
}
